package com.example.servicehi.resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * @author dongqun
 * @Description:
 * @date 2018/11/11 21:22
 */
@RestController
public class HiController {
    @Value("${server.port}")
    String port;
    @RequestMapping("/hi")
    public String home(){
        return "hi: i am from port:"+port;
    }

    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @RequestMapping("/hello")
    public String hello(){
        return "hello you";
    }

    @GetMapping("/getPrinciple")
    public OAuth2Authentication getPrinciple(OAuth2Authentication oAuth2Authentication,
                                             Principal principal, Authentication authentication){
        System.out.println("-----------------------");
        System.out.println(oAuth2Authentication.getUserAuthentication().getAuthorities().toString());
        System.out.println(oAuth2Authentication.toString());
        System.out.println("getName:"+principal.getName());
        System.out.println("toString"+principal.toString());
        System.out.println(authentication.getAuthorities().toString());
        return oAuth2Authentication;
    }

}
